package com.android.asportoonline;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class DettagliLocaleActivity extends Activity {
	
	private static final String SOAP_ACTION = null;
	private static final String IP = "10.0.2.2";
	private static final String URL = "http://" + IP
			+ ":8080/AsportoOnlineWS/AsportoOnlineWS?WSDL";
	private static final String NAMESPACE = "http://beans.business.asportoonline.org/";

	public List<String> recuperaDettagliLocale(String locale) {
		SoapObject req = new SoapObject(NAMESPACE, "recuperaDettagliLocale");
		req.addProperty("locale", locale);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER10);
		envelope.setOutputSoapObject(req);
		HttpTransportSE transport = new HttpTransportSE(URL);
		transport.debug = true;
		Object res = null;
		List<String> listaDettagliLocale = new ArrayList<String>();

		try {
			transport.call(SOAP_ACTION, envelope);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		}
		
		SoapObject o = (SoapObject)envelope.bodyIn;
		for (int i = 0; i < 10; i++) {
			listaDettagliLocale.add(o.getProperty(i).toString());
		}
		return listaDettagliLocale;
	}
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	StrictMode.enableDefaults();
    	super.onCreate(savedInstanceState);
        
		final String locale = getIntent().getExtras().getString(
				getPackageName() + ".locale");
        
        setContentView(R.layout.dettaglilocaleactivity);
        
        List<String> dettagliLocale = (List<String>)recuperaDettagliLocale(locale);
                
        java.net.URL newurl = null;
		try {
			newurl = new java.net.URL("http://" + IP + ":8080/AsportoOnline-war/img/" + dettagliLocale.get(9));
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
        Bitmap mIcon_val = null;
		try {
			mIcon_val = BitmapFactory.decodeStream(newurl.openConnection().getInputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
                
        ImageView immagine = new ImageView(this);
        immagine = (ImageView) findViewById(R.id.immagineLocale);
        
        immagine.setImageBitmap(mIcon_val);
        
        TextView nomeLocale = new TextView(this);
        nomeLocale = (TextView) findViewById(R.id.nomeLocale);        
        nomeLocale.setText(dettagliLocale.get(0));
        
        TextView indirizzoVia = new TextView(this);
        indirizzoVia = (TextView) findViewById(R.id.indirizzoVia);
        indirizzoVia.setText(dettagliLocale.get(1).toString());
        
        TextView indirizzoCapCitta = new TextView(this);
        indirizzoCapCitta = (TextView) findViewById(R.id.indirizzoCapCitta);
        indirizzoCapCitta.setText(dettagliLocale.get(2));
        
        TextView telefono = new TextView(this);
        telefono = (TextView) findViewById(R.id.telefono);
        telefono.setText(dettagliLocale.get(3));
        
        TextView email = new TextView(this);
        email = (TextView) findViewById(R.id.email);
        email.setText(dettagliLocale.get(4));
        
        TextView orarioPrimaApertura = new TextView(this);
        orarioPrimaApertura = (TextView) findViewById(R.id.orarioPrimaApertura);
        orarioPrimaApertura.setText(dettagliLocale.get(5));
        
        TextView orarioSecondaApertura = new TextView(this);
        orarioSecondaApertura = (TextView) findViewById(R.id.orarioSecondaApertura);
        orarioSecondaApertura.setText(dettagliLocale.get(6));
        
        TextView chiusuraGiorno = new TextView(this);
        chiusuraGiorno = (TextView) findViewById(R.id.chiusuraGiorno);
        chiusuraGiorno.setText(dettagliLocale.get(7));
        
        TextView consegna = new TextView(this);
        consegna = (TextView) findViewById(R.id.consegna);
        consegna.setText(dettagliLocale.get(8));
        
        Button buttonVisualizzaProdotti = (Button) findViewById(R.id.buttonVisualizzaProdotti);
        buttonVisualizzaProdotti.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				Intent intent = new Intent("com.android.ELENCOPRODOTTIACTIVITY");

				intent.putExtra(getPackageName() + ".locale", locale);

				startActivity(intent);
			}
		});
    }
    
}
